package com.git_taoxian.test1;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class Test {
    public static void main(String[] args) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
//        获取Student类的字节码文件对象
        Class<Student> clz = Student.class;
        Student stu = null;
        try {
//        获取共有 全参构造方法
            Constructor<Student> constructor = clz.getConstructor(String.class, int.class);
//            进行初始化
            stu = constructor.newInstance("柳岩", 17);
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } finally {
        }
        System.out.println(stu.getName()+","+stu.getAge());
//        反射获取show()方法
        Method method = clz.getMethod("show");
//        调用该show()方法
        Student rs = (Student) method.invoke(stu);
//        打印结果
        System.out.println(rs);
    }

}
